package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.scene.Node;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jmex.effects.LensFlare;
import com.jmex.effects.LensFlareFactory;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleImage;
import com.myjavautils.swing.SwingComponentFactory;

public class CreateLensFlarePanel extends JPanel {

	private		SimpleImage		mcl_0;
	private		SimpleImage		mcl_1;
	private		SimpleImage		mcl_2;
	private		SimpleImage		mcl_3;
	
	public	CreateLensFlarePanel(final Node cl_node, final JDialog cl_par) {
		setLayout(new MigLayout("inset 5"));
		
		mcl_0 = makeIm("Add Image 0", "split 4", null);
		mcl_1 = makeIm("Add Image 1", null, "wrap");
		mcl_2 = makeIm("Add Image 2", "split 4", null);
		mcl_3 = makeIm("Add Image 3", null, "wrap");
		
		JButton		lcl_create = SwingComponentFactory.makeAButton("Create");
		lcl_create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GameTaskQueueManager.getManager().update(new Callable<Void>() {
					public Void call() throws Exception {
						makeLensFlare(cl_node);
						return null;
					}
				});
				cl_par.setVisible(false);
			}
		});
		
		JButton		lcl_cancel = SwingComponentFactory.makeAButton("Cancel");
		lcl_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_par.setVisible(false);
			}
		});
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_cancel, lcl_create);
	}
	
	private		SimpleImage		makeIm(String str_btnName, String str_comm1, String str_comm2) {
		final	SimpleImage		lcl_im = new SimpleImage();
		JButton			lcl_btn = SwingComponentFactory.makeAButton(str_btnName);
		lcl_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//File		lcl_f = SceneWorkerPreferences.inst().getAFile(lcl_im, false, false);
				File		lcl_f = GenericUtils.getAnImageFile(lcl_im);
				if(lcl_f == null) {
					return;
				}
				
				try {
					BufferedImage		lcl_image = ImageIO.read(lcl_f);
					lcl_im.setImage(lcl_image);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		if(str_comm1 != null) {
			add(lcl_im, str_comm1);
		} else {
			add(lcl_im);
		}
		
		if(str_comm2 != null) {
			add(lcl_btn, str_comm2);
		} else {
			add(lcl_btn);
		}
		
		
		return lcl_im;
	}
	
	private	void	makeLensFlare(Node cl_node) {
		TextureState[] tex = new TextureState[4];
        tex[0] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[0].setTexture(TextureManager.loadTexture(mcl_0.getImage(), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true));
        tex[0].setEnabled(true);

        tex[1] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[1].setTexture(TextureManager.loadTexture(mcl_1.getImage(), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true));
        tex[1].setEnabled(true);

        tex[2] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[2].setTexture(TextureManager.loadTexture(mcl_2.getImage(), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true));
        tex[2].setEnabled(true);

        tex[3] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[3].setTexture(TextureManager.loadTexture(mcl_3.getImage(), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true));
        tex[3].setEnabled(true);

        LensFlare		flare = LensFlareFactory.createBasicLensFlare("flare", tex);
        flare.setRootNode(cl_node);
        flare.updateRenderState();
        flare.setModelBound(new BoundingSphere());
        flare.updateGeometricState(0, true);
        
        // notice that it comes at the end
        cl_node.attachChild(flare);
        cl_node.updateRenderState();
        cl_node.updateGeometricState(0, true);
	}
}

